package com.jpyl.jpMedicalTreatment.view.widget.sortlistview;

import android.app.Activity;
import android.app.Dialog;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.model.PopwindowModel;
import com.jpyl.jpMedicalTreatment.view.adapter.PopupWindowAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 15697 on 2019/3/8.
 */

public class MyPopupwindow extends Dialog {
    List<PopwindowModel> list = new ArrayList<>();
    private Activity mActivity;
    ListView listView;
    Button btn;
    PopupWindowAdapter adapter;
    private WindowManager.LayoutParams layoutParams;
    private DisplayMetrics dm;

    public MyPopupwindow(Activity context, final List<PopwindowModel> lists) {
        /* 设计Dialog样式 */
        super(context, R.style.dialog);//,
        this.mActivity = context;
        View view = View.inflate(mActivity, R.layout.activity_bottom_popwindow, null);
        setContentView(view);
        // 设置布局显示位置、宽度、透明度、是否浮层
        layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.NO_GRAVITY;
        dm = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(dm);
        layoutParams.width = dm.widthPixels;
        layoutParams.alpha = 1.0f;
        getWindow().setAttributes(layoutParams);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//让该window后所有的东西都成暗淡
        this.list = lists;
        listView = view.findViewById(R.id.lv_bottomPopWindow);
        btn = view.findViewById(R.id.btn_bottomPopWindow_cancel);
        adapter = new PopupWindowAdapter(context, list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (listener != null) {
                    listener.selectPositon(i);
                }

            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }


    public interface MyPopwindowListener {
        void selectPositon(int i);
    }

    public void setCallBack(MyPopwindowListener listener) {
        this.listener = listener;
    }

    private MyPopwindowListener listener;
}
